package swing;


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

import com.l2fprod.common.swing.JOutlookBar;
import smarthouse.Room;
import devices.AbstractDevice;
import annotations.Device;

public class SmartHouseFrame extends JFrame implements ActionListener{
	
	Vector<AbstractDevice> devices = new Vector<AbstractDevice>();
	Vector<Container> commandPanelsForEachTab = new Vector<Container>();
	
	Vector<Vector<AbstractDevice>> devicesPerRooms = new Vector<Vector<AbstractDevice>>();
	
	public SmartHouseFrame(Vector<AbstractDevice> devices){
		this.devices = devices;
		
		setLayout(new GridLayout(1,1));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Smarthouse desktop client");
		
		JTabbedPane tabbedPane = new JTabbedPane();
		
		
		Room[] rooms = Room.values();
		
		
		
		for (int i=0;i<rooms.length;i++) {
			devicesPerRooms.add(new Vector<AbstractDevice>());
			for (int j=0;j<devices.size();j++) { 
				if (devices.get(j).getRoom().equals(rooms[i]))
					devicesPerRooms.get(i).add(devices.get(j));
			}
			
			
			tabbedPane.addTab(rooms[i].getName(), createRoomGui(rooms[i],i));
		}
		
		add(tabbedPane);
		setSize(800, 300);
		setVisible(true);
	}
	
	private Component createRoomGui(Room r, int roomIndex) {
		JPanel roomPanel = new JPanel();
		roomPanel.setLayout(new GridLayout(1,1));		
		
		JOutlookBar bar = new JOutlookBar();
		


		for (int i=0;i<devicesPerRooms.get(roomIndex).size();i++) {
			Class<?> device = devicesPerRooms.get(roomIndex).get(i).getClass();
			
			Device annotation = (Device) device.getAnnotation(Device.class);
			String deviceName = annotation.name();
			String deviceDescription = annotation.description();
			String deviceIconURL = annotation.iconURL();		
			
			JButton button = new JButton(deviceName);
			button.addActionListener(this);
			button.setActionCommand(i+"");
			button.setName(roomIndex+"");
			button.setToolTipText(deviceDescription);
			Component commandList = createGui(devicesPerRooms.get(roomIndex).get(i),roomIndex);
			bar.addTab(deviceName, null, commandList, deviceDescription);
		}
		
		ScrollPane leftSP = new ScrollPane();
		ScrollPane rightSP = new ScrollPane();
		leftSP.add(bar);
		commandPanelsForEachTab.add(rightSP);
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSP, rightSP);
		splitPane.setDividerLocation(leftSP.getWidth()*2+leftSP.getVScrollbarWidth());
				
		roomPanel.add(splitPane);
		roomPanel.setSize(500, 500);
		return roomPanel;
	}

	public DevicePanel createGui(AbstractDevice device, int panelIndex){

		
		Class<?> deviceClass = device.getClass();
		if (deviceClass.getClass().isAnnotationPresent(Device.class))
			throw new RuntimeException("Command not annotated");
		try {
			DevicePanel df = new DevicePanel(device, this, panelIndex);
			return df;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton b =(JButton) e.getSource();
		int deviceIndex = Integer.parseInt(b.getActionCommand());
		AbstractDevice device = devices.get(deviceIndex);
		
		int panelIndex = Integer.parseInt(b.getName());
		createGui(device, panelIndex);
		
	}
	
	public void displayCommandGui(CommandFrame cf, int panelIndex){
		commandPanelsForEachTab.get(panelIndex).removeAll();
		commandPanelsForEachTab.get(panelIndex).add(cf);
		commandPanelsForEachTab.get(panelIndex).repaint();
	}

}
